package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import model.Expense;
import model.Income;
import model.Transaction;
import controller.Controller;
import java.awt.Insets;

@SuppressWarnings("serial")
public class TransactionItem extends JPanel {

	private JLabel lblFirstLine;
	private JLabel lblSecondLine;
	private JButton btnDelete;
	private Object transaction;
	private Controller controller;
	private TitledBorder border;
	private JButton btnEdit;

	public TransactionItem(Object transaction, Controller controller) {
		border = new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Transaction type", TitledBorder.LEADING, TitledBorder.TOP, null, null);
		setBorder(border);

		this.transaction = transaction;
		this.controller = controller;

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {0, 0, 0};
		gridBagLayout.rowHeights = new int[] {0, 0, 0};
		gridBagLayout.columnWeights = new double[] { 1.0, 0.0, 0.0 };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0 };
		setLayout(gridBagLayout);

		lblSecondLine = new JLabel("Category, Date");
		GridBagConstraints gbc_lblCostDate = new GridBagConstraints();
		gbc_lblCostDate.insets = new Insets(0, 0, 5, 0);
		gbc_lblCostDate.gridwidth = 3;
		gbc_lblCostDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblCostDate.gridx = 0;
		gbc_lblCostDate.gridy = 1;
		add(lblSecondLine, gbc_lblCostDate);

		lblFirstLine = new JLabel("Amount @ Venue");
		GridBagConstraints gbc_lblTransaction = new GridBagConstraints();
		gbc_lblTransaction.insets = new Insets(0, 0, 5, 0);
		gbc_lblTransaction.gridwidth = 3;
		gbc_lblTransaction.fill = GridBagConstraints.BOTH;
		gbc_lblTransaction.gridx = 0;
		gbc_lblTransaction.gridy = 0;
		add(lblFirstLine, gbc_lblTransaction);
		
		btnEdit = new JButton("\u270E");
		GridBagConstraints gbc_btnf = new GridBagConstraints();
		gbc_btnf.insets = new Insets(0, 0, 0, 5);
		gbc_btnf.gridx = 1;
		gbc_btnf.gridy = 2;
		add(btnEdit, gbc_btnf);

		
		btnDelete = new JButton("\u2717");
		GridBagConstraints gbc_btnDelete = new GridBagConstraints();
		gbc_btnDelete.gridx = 2;
		gbc_btnDelete.gridy = 2;
		add(btnDelete, gbc_btnDelete);

		
		appendActionListener();
		parseFieldValues();
	}

	private void appendActionListener() {
		btnDelete.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				controller.getModel().removeTransaction(transaction);
			}
		});

	}

	private void parseFieldValues() {
		Transaction t = (Transaction) transaction;
		String date = t.getDay() + "." + (t.getMonth()+1) + "." + t.getYear();
		
		if (transaction instanceof Expense) {
			border.setTitle("Ausgabe");
			
			Expense expense = (Expense) transaction;
			String category = controller.getModel().getDataSource()
					.getCategories().get(expense.getCategory());

			lblFirstLine.setText(expense.getAmount() + " bei " + expense.getVenue());
			lblSecondLine.setText(category+", " + date);
		}
		else if(transaction instanceof Income) {
			border.setTitle("Einkommen");
			
			Income income = (Income) transaction;
			
			lblFirstLine.setText(income.getAmount() + " bei "+income.getVenue());
			lblSecondLine.setText(date);
		}

	}
}
